﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MasterDetailTemplate.Services;

namespace MasterDetailTemplate.ViewModels
{
    public class HealthInfoViewModel : ViewModelBase
    {


        /// <summary>
        /// 依赖注入，注入 HealthInfoService
        /// </summary>
        /// <param name="healthInfoService"></param>
        public HealthInfoViewModel(IHealthInfoService healthInfoService)
        {
            _healthInfoService = healthInfoService;
        }

        /// <summary>
        /// 资讯的标题
        /// </summary>
        private string _title;
        public string Title
        {
            get => _title;
            set => Set(nameof(Title), ref _title, value);

        }

        /// <summary>
        /// 资讯的描述
        /// </summary>
        private string _description;
        public string Description
        {
            get => _description;
            set => Set(nameof(Description), ref _description, value);
        }

        /// <summary>
        /// 资讯的图片链接
        /// </summary>
        private string _picUrl;
        public string PicUrl
        {
            get => _picUrl;
            set => Set(nameof(PicUrl), ref _picUrl, value);
        }

        /// <summary>
        /// 资讯的详情链接
        /// </summary>
        private string _url;
        public string Url
        {
            get => _url;
            set => Set(nameof(Url), ref _url, value);
        }


        private IHealthInfoService _healthInfoService;


        /// <summary>
        /// 用于展示资讯的命令
        /// </summary>
        private RelayCommand _exhibitCommand;
        public RelayCommand ExhibitCommand =>
            _exhibitCommand ?? (_exhibitCommand = new RelayCommand(async () => {

                var result = await _healthInfoService.GetHealthInfoAsync();
                Description = result.Description;
                PicUrl = result.PicUrl;
                Title = result.Title;
                Url = result.Url;
            }));

        /// <summary>
        /// 显示资讯详情命令
        /// </summary>
        private RelayCommand _detailCommand;
        public RelayCommand DetailCommand =>
            _detailCommand ??
            (_detailCommand = new RelayCommand(async () =>
            {
                await DetailCommandFunction();
            }));
        internal async Task DetailCommandFunction() =>
            await _healthInfoService.OpenAsync(Url);
    }
}
